package com.zong;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zong.util.JsoupUtil;
import com.zong.util.PageData;
import com.zong.zdb.service.JdbcCodeService;
import com.zong.zdb.util.ZDBConfig;

/**
 * @desc 爬取广东省行政区划
 * @author zong
 * @date 2018年5月9日
 */
public class Test {
	private static JdbcCodeService codeService = JdbcCodeService.getInstance();
	private static final Logger LOGGER = LoggerFactory.getLogger(Test.class);
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static void main(String[] args) {
		try {
			ZDBConfig.readConfig();
			String url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/44.html";
			parse(url);
			System.out.println("====end");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void parse(String url) {
		PageData pd = new PageData();
		pd.put(JsoupUtil.CRAW_URL, url);
		pd.put(JsoupUtil.CRAW_ITEM, ".citytr,.countytr,.towntr,.villagetr");
		pd.put("unitid", "td:eq(0);text;;string");
		pd.put("unitname", "td:eq(1);text;;string");
		pd.put("village", "td:eq(2);text;;string");
		pd.put("suburl", "td:eq(0)>a;attr;href;string");
		try {
			PageData data = JsoupUtil.parseList(pd);
			List<PageData> list = (List<PageData>) data.get("data");
			for (PageData pageData : list) {
				// 存入数据库
				com.zong.zdb.util.PageData p = new com.zong.zdb.util.PageData();
				p.put("unitid", pageData.getString("unitid"));
				p.put("unitname", pageData.getString("village").equals("") ? pageData.getString("unitname")
						: pageData.getString("village"));
				try {
					codeService.insert("health242-gdadmin", "TEST_UNIT", p);
				} catch (Exception e) {
					e.printStackTrace();
				}
				// 解析子节点
				if (!pageData.get("suburl").equals("")) {
					System.out.println(pageData.getString("suburl"));
					parse(pageData.getString("suburl"));
				}
			}
			System.out.println(list);
			try {
				codeService.update("health242-gdadmin", "TEST_UNIT_LOG", new com.zong.zdb.util.PageData("status", "1"),
						new com.zong.zdb.util.PageData("url", url));
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} catch (Exception e) {
			// 插入解析错误日志
			com.zong.zdb.util.PageData p = new com.zong.zdb.util.PageData();
			p.put("url", url);
			p.put("create_date", dateFormat.format(new Date()));
			p.put("status", "0");
			try {
				codeService.insert("health242-gdadmin", "TEST_UNIT_LOG", p);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			LOGGER.error("解析错误:{url}", url);
			LOGGER.error(e.toString(), e);
		}
	}
}
